package com.mapbased.da;

import java.util.Map;
import com.mapbased.wfw.RenderContext;
import java.util.Set;

/**
 * <p>Title: </p>
 *
 * <p>Description: </p>
 *
 * <p>Copyright: Copyright (c) 2004</p>
 *
 * <p>Company: </p>
 *
 * @author chy(Baily)
 * @version 1.0
 */
public class Feature
{
    public static final Object[] EMPTY_VALUES=new Object[]
        {};

    /**
     * Key :field name in String
     * Value:Integer value of FieldIndex
     */
    private final Map fieldIndexMap;
    public Feature(Map fm)
    {
        this.fieldIndexMap=fm;
    }

    Object[] values=EMPTY_VALUES;;
    public Object getValue(int index)
    {
        if(index >= 0)
        {
            return this.values[index];
        }
        return null;

    }

    public void addToRenderContext(RenderContext rc)
    {
        Map m=this.fieldIndexMap;
        Map rmap=rc.getRequestMap();

        for(java.util.Iterator i=m.entrySet().iterator();i.hasNext();)
        {
            Map.Entry e=(Map.Entry)i.next();
            rmap.put(e.getKey(),this.values[((Number)e.getValue()).intValue()]);

        }

    }

    public Object[] getValues()
    {
        return this.values;
    }

    public void setValue(int index,Object value)
    {
        this.values[index]=value;
    }

    private int getFieldIndex(String key)
    {
        Object o=this.fieldIndexMap.get(key);
        return((Integer)o).intValue();
    }

    public Object getValue(String key)
    {
        int i=getFieldIndex(key);

        return this.getValue(i);
    }

    public int getValueInt(String key)
    {
        Object o=this.getValue(key);
        if(o == null)
        {
            return 0;
        }
        return((Number)o).intValue();
    }

    public long getValueLong(String key)
    {
        Object o=this.getValue(key);
        if(o == null)
        {
            return 0l;
        }
        return((Number)o).longValue();
    }

    public boolean getValueBoolean(String key)
    {
        Object o=this.getValue(key);
        if(o == null)
        {
            return false;
        }
        return((Boolean)o).booleanValue();
    }

    public long getValueLongFromDate(String key)
    {
        Object o=this.getValue(key);
        if(o == null)
        {
            return 0l;
        }
        return((java.util.Date)o).getTime();
    }

//   private static final java.text.SimpleDateFormat dd;
//    public String getValueStrFromDate(String key)
//    {
//	long l=this.getValueLongFromDate(key);
//	if(l==0l)
//	{
//	    return "";
//	}
//
//    }

    public void setValue(String key,Object value)
    {

        int i=getFieldIndex(key);
        this.setValue(i,value);

    }

    public String getValueHtmlJsSafe(String key)
    {
        Object o=this.getValue(key);
        if(o == null)
        {
            return "";
        }
        return com.mapbased.wfw.Util.toJsSafe(
            com.mapbased.wfw.Util.replaceToXml(o.toString()));

    }

    public String getValueHtmlSafe(String key)
    {
        Object o=this.getValue(key);
        if(o == null)
        {
            return "";
        }
        return com.mapbased.wfw.Util.replaceToXml(o.toString());

    }

    public String getValueJsSafe(String key)
    {
        Object o=this.getValue(key);
        if(o == null)
        {
            return "";
        }
        return com.mapbased.wfw.Util.toJsSafe(o.toString());

    }

    public String getValueUrlSafe(String key)
    {
        Object o=this.getValue(key);
        if(o == null)
        {
            return "";
        }
        try
        {
            return java.net.URLEncoder.encode(o.toString(),"UTF-8");
        }
        catch(java.lang.Exception e)
        {
            e.printStackTrace();
            return o.toString();
        }
    }

    public String getValueUrlSafeExcludeDelimiter(String key)
    {

        Object o=this.getValue(key);
        if(o == null)
        {
            return "";
        }
        try
        {


            return  com.mapbased.wfw.Util.urlEncodeExcludeDelimiter(o.toString(),"UTF-8");
        }
        catch(java.lang.Exception e)
        {
            e.printStackTrace();
            return o.toString();
        }

    }

}
